﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuizAppMvc2.Models;

namespace QuizAppMvc2.Controllers
{
    public class QuestaoController : Controller
    {
        //
        // GET: /Questao/
        quizdbEntities _db = new quizdbEntities();

        public ActionResult Index()
        {

            //return View(_db.Questaos.OfType<QuestaoSimple>());
            return View(_db.Questaos);
        }

        //
        // GET: /Questao/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Questao/Create

        public ActionResult Create()
        {
            NewQuestionModels m = new NewQuestionModels();

            m.gabaritos = new List<NewQuestionModels.GabaritoItem>();
            m.gabaritos.Add(new NewQuestionModels.GabaritoItem() { id = 1, descricao = "Certo" });
            m.gabaritos.Add(new NewQuestionModels.GabaritoItem() { id = 2, descricao = "errado" });

            m.materias = _db.Materias.ToList<Materia>();
            return View(m);
        }



        public ActionResult CreateComplex()
        {
            QuestaoComAlternativasModels m = new QuestaoComAlternativasModels();

            m.materias = _db.Materias.ToList<Materia>();
            m.alternativas = new List<QuestaoComAlternativasModels.Alternativa>();
            m.alternativas.Add( new QuestaoComAlternativasModels.Alternativa() { certo = false, texto = "Alternativa 1"});
            m.alternativas.Add(new QuestaoComAlternativasModels.Alternativa() { certo = false, texto = "Alternativa 2" });
            m.alternativas.Add(new QuestaoComAlternativasModels.Alternativa() { certo = false, texto = "Alternativa 3" });
            m.alternativas.Add(new QuestaoComAlternativasModels.Alternativa() { certo = false, texto = "Alternativa 4" });

            return View(m);
        }

        [HttpPost]
        public ActionResult CreateComplex(QuestaoComAlternativasModels newQuestion)
        {
            try
            {
                QuestaoMultipla q = new QuestaoMultipla();
                q.idMateria = (int) newQuestion.materiaid;
                q.idTopico = newQuestion.topicoid;
                q.idSubTopico = newQuestion.subtopico;
                q.Texto = newQuestion.texto;
                q.TipoQuestao = 2;
                q.Gabarito = newQuestion.gabarito;
                q.DataCriacao = DateTime.Now;
                int i = 1;
                foreach( QuestaoComAlternativasModels.Alternativa a in  newQuestion.alternativas)
                {
                    q.QuestaoAlternativas.Add(new QuestaoAlternativa() { Texto = a.texto , Alternativa = i});
                    i++;
                }

                _db.AddToQuestaos(q);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        } 

        //
        // POST: /Questao/Create

        [HttpPost]
        public ActionResult Create(NewQuestionModels newQuestion)
        {
            try
            {
                QuestaoSimple qsimples = new QuestaoSimple();
                qsimples.idMateria = (int) newQuestion.materiaid;
                qsimples.idTopico = newQuestion.topicoid;
                qsimples.idSubTopico = newQuestion.subtopico;
                qsimples.Texto = newQuestion.texto;
                qsimples.TipoQuestao = 1;
                qsimples.Gabarito = newQuestion.gabarito;
                qsimples.DataCriacao = DateTime.Now;

                _db.AddToQuestaos(qsimples);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
        
        //
        // GET: /Questao/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Questao/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Questao/Delete/5
 
        public ActionResult Delete(int id)
        {
            var questaoToDelete = (from m in _db.QuestaoSimplesViews

                                   where m.idQuestao == id

                                   select m).First();
            return View(questaoToDelete);
        }

        //
        // POST: /Questao/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var questaoToDelete = (from m in _db.Questaos

                                       where m.idQuestao == id

                                       select m).First();

                _db.DeleteObject(questaoToDelete);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult abc(int year)
        {
            var v = _db.Topicoes.Where(x => x.idMateria == year);
            //var v = db.Topicoes;

            return Json(
                v.Select(x => new { value = x.idTopico, novo = x.Nome }),
                JsonRequestBehavior.AllowGet
            );
        }

        public ActionResult Topicos(int idMateria)
        {
            quizdbEntities db = new quizdbEntities();
            var v = _db.Topicoes.Where(e => e.idMateria == idMateria);
            return Json(v, JsonRequestBehavior.AllowGet);
            //return Json(
            //    Enumerable.Range(1, 12).Select(x => new { value = x, text = x }),
            //    JsonRequestBehavior.AllowGet
            //);
        }


        public ActionResult Subtopicos(int year)
        {
            
            var v = _db.SubTopicoes.Where(x => x.idTopico == year);
            //var v = db.Topicoes;

            return Json(
                v.Select(x => new { value = x.idSubTopico, novo = x.Nome }),
                JsonRequestBehavior.AllowGet
            );
        }
    }
}
